﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using netcore_comm.Redis;
using netcore_comm.SqlSugar;
using netcore_service.Dto;
using netcore_service.IRepository;
using netcore_service.IService;
using netcore_service.Model;
using SqlSugar;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace netcore_service.Service
{

    public class MeetSubmitInfoService : IMeetSubmitInfoService
    {
        private readonly ISqlSugarFactory _sqlSugarFactory;
        private readonly RedisHelper _redisHelper;
        private readonly IMeetSubmitInfoRepository _meetSubmitInfoRepository;
        public MeetSubmitInfoService(ISqlSugarFactory sqlSugarFactory, RedisHelper redisHelper, IMeetSubmitInfoRepository meetSubmitInfoRepository)
        {
            _sqlSugarFactory = sqlSugarFactory;
            _redisHelper = redisHelper;
            _meetSubmitInfoRepository = meetSubmitInfoRepository;
        }

        [Authorize]
        [HttpPost]
        public async Task<ApiResult<List<MeetSubmitInfo>>> MeetSubmitInfoListAsync(PagesModel pagesModel)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            RefAsync<int> i = 0;
            var pageList = await db.Queryable<MeetSubmitInfo>().ToOffsetPageAsync(pagesModel.page, pagesModel.pageSize, i);
            return APIResult.GetSuccess<List<MeetSubmitInfo>>(pageList, i);
        }
        [Authorize(Roles = "mobile")]
        [HttpPost]
        public async Task<ApiResult<MeetSubmitInfo>> MeetSubmitInfoTopOne()
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            var list = await _meetSubmitInfoRepository.GetListAsync(db);
            return APIResult.GetSuccess<MeetSubmitInfo>(list.First());
        }

        [Authorize]
        [HttpPost]
        public async Task<ApiResult<bool>> MeetSubmitInfoUpdateOrAddAsync(MeetSubmitInfo meetSubmitInfo)
        {
            if (meetSubmitInfo == null)
            {
                return APIResult.GetError<bool>("提交失败");
            }
            var db = _sqlSugarFactory.GetInstance(nameof(netData), DBOperateType.Write);
            if (meetSubmitInfo.Id == 0)
            {
                meetSubmitInfo.modifyDate = DateTime.Now;
                return APIResult.GetSuccess<bool>(await _meetSubmitInfoRepository.InsertAsync(db, meetSubmitInfo));
            }
            else
            {
                meetSubmitInfo.modifyDate = DateTime.Now;

                return APIResult.GetSuccess<bool>(await _meetSubmitInfoRepository.UpdateAsync(db, meetSubmitInfo));
            }
        }

        [Authorize]
        [HttpPost]
        public async Task<ApiResult<MeetSubmitInfo>> MeetSubmitInfoByIdAsync(int id)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            return APIResult.GetSuccess<MeetSubmitInfo>(await _meetSubmitInfoRepository.GetModelAsync(db, e => e.Id == id));
        }

        /// <summary>
        /// 查询手机号是否在列表内
        /// </summary>
        /// <param name="contact">手机号</param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<ApiResult<MeetSubmitInfo>> MeetSubmitInfoByContactAsync(string contact)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            var list = await _meetSubmitInfoRepository.GetListAsync(db, e => e.contact == contact);
            if (list != null)
            {
                int count = list.Count;
                return APIResult.GetSuccess<MeetSubmitInfo>(list.FirstOrDefault(), count);
            }
            else
            {
                return APIResult.GetSuccess<MeetSubmitInfo>(new MeetSubmitInfo(), 0);
            }

        }

        [Authorize]
        [HttpPost]
        public async Task<ApiResult<bool>> MeetSubmitInfoDelAsync(int id)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData), DBOperateType.Write);
            return APIResult.GetSuccess<bool>(await _meetSubmitInfoRepository.DeleteAsync(db, e => e.Id == id));
        }



    }
}
